<div class="wrapper-bz-page">
    <div class="wrapper-bz-body">
        <div class="column-block block-left" style="width:600px;">
            <div class="column-title">活动</div>
            <div class="column-content">
                <div class="column-filter">

                </div>
                <div class="column-datagrid">
                    <table id="table_clockin_activity_in_m71" style="width:100%;height:100%"></table>
                </div>
            </div>
        </div>

        <div class="column-block" style="left:610px; width:120px;">
            <div class="column-title">日期</div>
            <div class="column-content no-filter">
                <ul id="list_clockin_activity_plan_date" class="box-datelist"></ul>
            </div>
        </div>

        <div class="column-block" style="left:740px; width:300px;">
            <div class="column-title">电影单词</div>
            <div class="column-content no-filter">
                <div class="column-datagrid">
                    <table id="table_movie_word_in_m71" style="width:100%;height:100%"></table>
                </div>
            </div>
        </div>

        <div class="column-block block-right" style="left:1050px;">
            <div class="column-title">打卡任务</div>
            <div class="column-content">
                <div class="column-filter">

                </div>
                <div class="column-datagrid no-filter">
                    <table id="table_clockin_activity_plan_in_m71" style="width:100%;height:100%"></table>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function(){
        var subject_1 = "clockin_activity_in_m71";
        var subject_2 = "movie_word_in_m71";
        var subject_3 = "clockin_activity_plan_in_m71";
        //列表-对话框-表单
        var tableId_1 = '#table_' + subject_1;
        var tableId_2 = '#table_' + subject_2;
        var tableId_3 = '#table_' + subject_3;

        var tableObject_1 = $(tableId_1);
        var tableObject_2 = $(tableId_2);
        var tableObject_3 = $(tableId_3);

        var addDialogId_1 = '#dialog_add_' + subject_1;
        var addDialogId_2 = '#dialog_add_' + subject_2;
        var addDialogId_3 = '#dialog_add_' + subject_3;
        var editDialogId_2 = '#dialog_edit_' + subject_2;
        var editDialogId_3 = '#dialog_edit_' + subject_3;

        var addFormId_1 = '#form_add_' + subject_1;
        var addFormId_2 = '#form_add_' + subject_2;
        var addFormId_3 = '#form_add_' + subject_3;
        var editFormId_2 = '#form_edit_' + subject_2;
        var editFormId_3 = '#form_edit_' + subject_3;

        var paramDataAdd_1 = {};
        var paramDataAdd_2 = {};
        var paramDataAdd_3 = {};
        var paramDataEdit_2 = {};
        var paramDataEdit_3 = {};

        var selectLeftMainId = 0;
        var selectPlanDate = 0;
        var configDialogAdd_1 = {
            href:'/admin/DatagridCustom/pageAdd',
            title:'新增数据表',
            queryParams:{},
            width:dialogDefaultWidth,
            height:dialogDefaultHeight,
            modal:true,
            buttons:[{
                text:'<i class="fa fa-check fa-btn-cls" style="color:green"></i>保存',
                plain:true,
                handler:function(){
                    paramDataAdd_1 = $(addFormId_1).serializeObject();
                    if(!paramDataAdd_1.datagrid_name){
                        layer.msg("请填写数据表名称");
                        return ;
                    }

                    if(!paramDataAdd_1.menu_id){
                        layer.msg("请选择菜单");
                        return ;
                    }
                    if(!paramDataAdd_1.table_id){
                        layer.msg("请选择数据表");
                        return ;
                    }

                    var loading = layer.load(1, {
                        shade: [0.1,'#fff'] //0.1透明度的白色背景
                    });
                    $.ajax({
                        url: '/api/DatagridCustom/addDatagridCustom',
                        async: false,
                        dataType:'json',
                        type:'post',
                        data:paramDataAdd_1,
                        success:function(resp){
                            layer.close(loading);
                            if( 1 == resp.code ){
                                $(addDialogId_1).dialog('close');
                                $(tableId_1).datagrid('reload');
                            }else{
                                layer.msg(resp.message);
                            }
                        }
                    });
                }
            },{
                text:'<i class="fa fa-times fa-btn-cls" style="color:red"></i>取消',
                plain:true,
                handler:function(){
                    $(addDialogId_1).dialog('close');
                }
            }]
        };

        var configDialogAdd_2 = {
            href:'/admin/DatagridColumn/pageAdd',
            title:'新增页面纵列',
            queryParams:{},
            width:dialogDefaultWidth,
            height:dialogDefaultHeight,
            modal:true,
            buttons:[{
                text:'<i class="fa fa-check fa-btn-cls" style="color:green"></i>保存',
                plain:true,
                handler:function(){
                    paramDataAdd_2 = $(addFormId_2).serializeObject();
                    paramDataAdd_2 = $.extend(paramDataAdd_2, {datagrid_id:selectLeftMainId});
                    console.log(paramDataAdd_2);
                    if(!paramDataAdd_2.column_name){
                        layer.msg("请填写列标识码");
                        return ;
                    }
                    if(!paramDataAdd_2.column_title){
                        layer.msg("请填写列名");
                        return ;
                    }
                    var loading = layer.load(1, {
                        shade: [0.1,'#fff'] //0.1透明度的白色背景
                    });
                    $.ajax({
                        url: '/api/DatagridColumn/addDatagridColumn',
                        async: false,
                        dataType:'json',
                        type:'post',
                        data:paramDataAdd_2,
                        success:function(resp){
                            layer.close(loading);
                            if( 1 == resp.code ){
                                $(addDialogId_2).dialog('close');
                                $(tableId_2).datagrid('reload');
                            }else{
                                layer.msg(resp.message);
                            }
                        }
                    });
                }
            },{
                text:'<i class="fa fa-times fa-btn-cls" style="color:red"></i>取消',
                plain:true,
                handler:function(){
                    $(addDialogId_2).dialog('close');
                }
            }]
        };
        var configDialogAdd_3 = {
            href:'/admin/DatagridToolbar/pageAdd',
            title:'新增页面工具栏',
            queryParams:{},
            width:dialogDefaultWidth,
            height:dialogDefaultHeight,
            modal:true,
            buttons:[{
                text:'<i class="fa fa-check fa-btn-cls" style="color:green"></i>保存',
                plain:true,
                handler:function(){
                    paramDataAdd_3 = $(addFormId_3).serializeObject();
                    paramDataAdd_3 = $.extend(paramDataAdd_3, {datagrid_id:selectLeftMainId});
                    console.log(paramDataAdd_3);
                    if(!paramDataAdd_3.button_type){
                        layer.msg("请填写类型");
                        return ;
                    }
                    if(!paramDataAdd_3.button_text){
                        layer.msg("请填写名称");
                        return ;
                    }
                    var loading = layer.load(1, {
                        shade: [0.1,'#fff'] //0.1透明度的白色背景
                    });
                    $.ajax({
                        url: '/api/DatagridToolbar/addPageToolbar',
                        async: false,
                        dataType:'json',
                        type:'post',
                        data:paramDataAdd_3,
                        success:function(resp){
                            layer.close(loading);
                            if( 1 == resp.code ){
                                $(addDialogId_3).dialog('close');
                                tableObject_3.datagrid('reload');
                            }else{
                                layer.msg(resp.message);
                            }
                        }
                    });
                }
            },{
                text:'<i class="fa fa-times fa-btn-cls" style="color:red"></i>取消',
                plain:true,
                handler:function(){
                    $(addDialogId_3).dialog('close');
                }
            }]
        };
        var configDialogEdit = {
            href:'role/edit',
            title:'修改角色',
            queryParams:{},
            width:dialogDefaultWidth,
            height:dialogDefaultHeight,
            modal:true,
            buttons:[{
                text:'<i class="fa fa-check fa-btn-cls" style="color:green"></i>保存',
                plain:true,
                handler:function(){
                    paramDataEdit = $(editFormId).serializeObject();
                    console.log(paramDataEdit);
                    if(!paramDataEdit.role_name){
                        layer.msg("请填写角色名称");
                        return ;
                    }
                    var loading = layer.load(1, {
                        shade: [0.1,'#fff'] //0.1透明度的白色背景
                    });
                    $.ajax({
                        url: '/api/role/edit',
                        async: false,
                        dataType:'json',
                        type:'post',
                        data:paramDataEdit,
                        success:function(resp){
                            layer.close(loading);
                            if( 1 == resp.code ){
                                $(editDialogId).dialog('close');
                                $(tableId).datagrid('reload');
                            }else{
                                layer.msg(resp.message);
                            }
                        }
                    });
                }
            },{
                text:'<i class="fa fa-times fa-btn-cls" style="color:red"></i>取消',
                plain:true,
                handler:function(){
                    $(editDialogId).dialog('close');
                }
            }]
        };
        var configDialogEdit_3 = {
            href:'/admin/DatagridToolbar/pageEdit',
            title:'编辑页面工具栏',
            queryParams:{},
            width:dialogDefaultWidth,
            height:dialogDefaultHeight,
            modal:true,
            buttons:[{
                text:'<i class="fa fa-check fa-btn-cls" style="color:green"></i>保存',
                plain:true,
                handler:function(){
                    paramDataEdit_3 = $(editFormId_3).serializeObject();
                    console.log(paramDataEdit_3);
                    if(!paramDataEdit_3.button_type){
                        layer.msg("请填写类型");
                        return ;
                    }
                    if(!paramDataEdit_3.button_text){
                        layer.msg("请填写名称");
                        return ;
                    }
                    var loading = layer.load(1, {
                        shade: [0.1,'#fff'] //0.1透明度的白色背景
                    });
                    $.ajax({
                        url: '/api/DatagridToolbar/editPageToolbar',
                        async: false,
                        dataType:'json',
                        type:'post',
                        data:paramDataEdit_3,
                        success:function(resp){
                            layer.close(loading);
                            if( 1 == resp.code ){
                                $(editDialogId_3).dialog('close');
                                tableObject_3.datagrid('reload');
                            }else{
                                layer.msg(resp.message);
                            }
                        }
                    });
                }
            },{
                text:'<i class="fa fa-times fa-btn-cls" style="color:red"></i>取消',
                plain:true,
                handler:function(){
                    $(editDialogId_3).dialog('close');
                }
            }]
        };

        var configDatagrid_1 = {
            url:'/api/ClockinActivity/getClockinActivityList',
            method: 'post',
            remoteSort:false,
            queryParams:{content_type:3},
            pageList:[25,50,100],
            pageSize:25,
            pagination:true,
            clickToEdit:false,
            dblclickToEdit:false,
            singleSelect:true,
            checkOnSelect:true,
            selectOnCheck:true,
            rownumbers:false,
            striped:true,
            fitColumns:true,
            fit:true,
            frozenColumns:[[
            ]],
            columns:[[
                {field:'id',title:'ID',width:40,hidden:true},
                {field:'activity_name',title:'活动名称',width:250},
                {field:'movie_name_cn',title:'电影名称',width:150},
                {field:'join_start_time',title:'报名开始时间',width:160},
            ]],
            loadFilter: function(data){
                return loadFilter(data, true);
            },
            onSelect: function(index, row){
                var clockinActivityDateList = getClockinActivityPlanDateList(row.id);
                var htmlDateList = assembleHtmlForDateList(clockinActivityDateList);
                $("#list_clockin_activity_plan_date").html(htmlDateList);

                configDatagrid_2.queryParams = {movie_id:row.subject_id};
                tableObject_2.datagrid(configDatagrid_2);
                selectLeftMainId = row.id;
            },
            onClickRow: function(index, row){
                // console.log("enter click row");
                // selectLeftMainId = row.id;
                // configDatagrid_2.queryParams = {datagrid_id:row.id};
                // tableObject_2.datagrid(configDatagrid_2);
            },
            onCheck: function(index, row){
            },
            onUncheck: function(index, row){
            },
            onLoadSuccess: function(){
                tableObject_1.datagrid('selectRow', 0);
            },
            toolbar:[
                {
                    text:'<span class="tool-btn-refresh"><i class="fa fa-refresh"></i>刷新</span>',
                    plain:true,
                    handler:function(){
                        tableObject_1.datagrid('reload');
                    }
                }
            ]
        };
        var configDatagrid_2 = {
            url:'/api/LibMovieWord/getLibMovieWordList',
            method: 'post',
            remoteSort:false,
            queryParams:{},
            pageList:[30,50,100],
            pageSize:30,
            pagination:true,
            clickToEdit:false,
            dblclickToEdit:true,
            singleSelect:false,
            checkOnSelect:false,
            selectOnCheck:false,
            rownumbers:false,
            striped:true,
            frozenColumns:[[
                {field:'ck',title:'ck',checkbox:true},
                {field:'id',title:'ID',width:40,hidden:true},

            ]],
            columns:[[
                {field:'spelling',title:'单词',width:150},
            ]],
            loadFilter: function(data){
                return loadFilter(data, true);
            },
            onCheck: function(row){
            },
            onUncheck: function(row){
            },
            onLoadSuccess:function(){
            },
            toolbar:[
                {
                    text:'<span class="tool-btn-refresh"><i class="fa fa-refresh"></i>刷新</span>',
                    plain:true,
                    handler:function(){
                        tableObject_2.datagrid('reload');
                    }
                },
                {
                    text:'<span class="tool-btn-add"><i class="fa fa-plus"></i>新增</span>',
                    plain:true,
                    handler:function(){
                        var rows = tableObject_2.datagrid("getChecked");
                        if(0 == rows.length){
                            layer.msg("请勾选要新增到打卡计划的单词");
                        }
                        var arrId = [];
                        $.each(rows, function(index, item){
                            arrId.push(item.id);
                        });
                        $.ajax({
                            url:'/api/ClockinActivityPlan/addClockinActivityPlan',
                            type:'post',
                            data: {clockin_activity_id:selectLeftMainId, plan_date:selectPlanDate, arr_subject_id: arrId},
                            dataType:'json',
                            async:false,
                            success:function(resp){
                                if(0 == resp.code){
                                    layer.msg(resp.message);
                                }else{
                                    tableObject_3.datagrid('reload');
                                }
                            }
                        });
                    }
                }
            ]

        };
        var configDatagrid_3 = {
            url:'/api/ClockinActivityPlanTask/getClockinActivityPlanTaskOption',
            method: 'post',
            remoteSort:false,
            queryParams:{},
            pageList:[30,50,100],
            pageSize:30,
            pagination:false,
            clickToEdit:false,
            dblclickToEdit:true,
            singleSelect:false,
            checkOnSelect:false,
            selectOnCheck:true,
            rownumbers:false,
            striped:true,
            fitColumns:true,
            fit:true,
            frozenColumns:[[
                {field:'ck',title:'ck',checkbox:true},
            ]],
            columns:[[
                {field:'id',title:'ID',width:40,hidden:true},
                {field:'spelling',title:'单词',width:100},
            ]],
            loadFilter: function(data){
                return loadFilter(data, false);
            },
            onCheck: function(index, row){
                var checkList = tableObject_3.datagrid('getChecked');

            },
            onUncheck: function(index, row){
                var checkList = tableObject_3.datagrid('getChecked');
                if(checkList.length > 1){
                    $("#btn_edit_in_page_toolbar").linkbutton('disable');
                }else if(1 == checkList.length){
                    $("#btn_edit_in_page_toolbar").linkbutton('enable');
                    $("#btn_delete_in_page_toolbar").linkbutton('enable');
                }else{
                    $("#btn_edit_in_page_toolbar").linkbutton('disable');
                    $("#btn_delete_in_page_toolbar").linkbutton('disable');
                }
            },
            onLoadSuccess: function(){
            },
            toolbar:[
                {
                    text:'<span class="tool-btn-refresh"><i class="fa fa-refresh"></i>刷新</span>',
                    plain:true,
                    handler:function(){
                        tableObject_3.datagrid('reload');
                    }
                },
                {
                    text:'<span class="tool-btn-delete"><i class="fa fa-trash"></i>删除</span>',
                    plain:true,
                    handler:function(){
                        var arrCheckId = getDatagridCheckedRowId(tableObject_3, "删除");
                        if(!arrCheckId){
                            return false;
                        }
                        var confirm = layer.confirm('你确定要删除?',{title:'执行操作',btn:['是','否']},function(){
                            $.ajax({
                                url:'/api/DatagridToolbar/deleteMultiDatagridToolbar',
                                type:'post',
                                data: {"arr_id": arrCheckId},
                                dataType:'json',
                                success:function(resp){
                                    layer.close(confirm);
                                    if(1 == resp.code){
                                        tableObject_3.datagrid('reload');
                                    }else{
                                        layer.msg(resp.message);
                                    }
                                }
                            });
                        });
                    }
                }
            ]

        };
        tableObject_1.datagrid(configDatagrid_1);

        // SearchCombobox('search_input_user_in_role','table_user_in_role',function (val,name) {
        //     configDatagrid_1.queryParams = {};
        //     configDatagrid_1.queryParams[name] = val;
        //     if(selectRoleId){
        //         configDatagrid_1.queryParams['role_id'] = selectRoleId;
        //     }
        //     tableObject_1.datagrid(configDatagrid_1);
        // },['ck','ID']);

        function getClockinActivityPlanDateList(clockinActivityId)
        {
            var dateList = [];
            $.ajax({
                url: '/api/ClockinActivity/getClockinActivityDateList',
                async: false,
                type:'post',
                data:{clockin_activity_id: clockinActivityId},
                success:function(resp){
                    if( 1 == resp.code ){
                        dateList = resp.data;
                    }
                }
            });
            return dateList;
        }

        function assembleHtmlForDateList(dateList)
        {
            var html = '';
            var className = "";
            $.each(dateList, function(index, item){
                if(item.plan_id > 0){
                    className = "plan-made";
                }else{
                    className = "plan-not-made";
                }

                if(1 == item.is_current){
                    className += ' today';
                }
                html += '<li class="item-clockin-activity-plan-date item-date '+ className +'" day="'+ item.date +'" >' + item.date + '</li>';
            });
            return html;
        }

        $(document).on("click", ".box-datelist .item-date", function(){
            var curItem = $(this);
            curItem.siblings(".item-date").removeClass("selected");
            curItem.toggleClass("selected");
            if(curItem.hasClass("selected")){
                selectPlanDate = curItem.attr("day");
            }else{
                selectPlanDate = '';
            }
            configDatagrid_3.queryParams = {content_type:3, clockin_activity_id:selectLeftMainId, plan_date:selectPlanDate};
            tableObject_3.datagrid(configDatagrid_3);
        });
    });
</script>